package com.eascs.archive.dao;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.archive.entity.ArchiveRecordEntity;
import com.eascs.archive.entity.ArchiveRecordRepeatEntity;
import com.eascs.archive.entity.ProvincePlatformEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class ArchiveRecordDao extends BaseO2oDao{
	
	public int insertArchiveRecord(ArchiveRecordEntity entity){
		if (entity == null) {
			return 0;
		}
		return o2oJdbcSession.insert(entity);
	}
	
	public int updateArchiveRecord(ArchiveRecordEntity entity){
		if (entity == null) {
			return 0;
		}
		return o2oJdbcSession.updateById(entity);
	}
	
	public int updateArchiveRecordContentJson(String id,String json){
		
		if (StringUtils.isBlank(id) || StringUtils.isBlank(json)) {
			return 0;
		}
		String sql="update o2o_archive_record set content_json =? where id=?";
		return o2oJdbcSession.execute(sql, new Object[]{json,id});
	}
	
	public ArchiveRecordEntity queryArchiveRecord(String loanapplyNo,String bankType,String formType){
		if (StringUtils.isBlank(loanapplyNo) || StringUtils.isBlank(bankType) || StringUtils.isBlank(formType)) {
			return null;
		}
		String sql="select * from o2o_archive_record where loanapply_no=? and bank_type_id=? and form_type_id=?";
		return o2oJdbcSession.findEntity(sql, ArchiveRecordEntity.class, new Object[]{loanapplyNo,bankType,formType});
	}
	
	public List<ArchiveRecordEntity> queryArchiveRecordByLoanapplyNo(String loanapplyNo){
		if (StringUtils.isBlank(loanapplyNo)) {
			return null;
		}
		String sql="select * from o2o_archive_record where loanapply_no=?";
		return o2oJdbcSession.findEntitys(sql, ArchiveRecordEntity.class, new Object[]{loanapplyNo});
	}
	
	
	public ProvincePlatformEntity queryProvinceInfoByLoanId(String loanapplyNo){
		String sql="SELECT opr.* FROM o2o_platform_regional opr,o2o_loanapply l WHERE opr.platform_id = l.ltd AND l.loanapply_no=?";
		return o2oJdbcSession.findEntity(sql, ProvincePlatformEntity.class, loanapplyNo);
	}
	
	
	
	public ArchiveRecordRepeatEntity queryArchiveRepeatRecordById(String id){
		if (StringUtils.isBlank(id)) {
			return null;
		}
		String sql="select * from o2o_archive_record_repeat where id=?";
		return o2oJdbcSession.findEntity(sql, ArchiveRecordRepeatEntity.class, new Object[]{id});
	}

	public boolean hasArchiveRepeatRecordByLoanApplyNo(String loanapplyNo){
		if (StringUtils.isBlank(loanapplyNo)) {
			return false;
		}
		String sql="select * from o2o_archive_record_repeat where loanapply_no=?";
		ArchiveRecordRepeatEntity record = o2oJdbcSession.findEntity(sql, ArchiveRecordRepeatEntity.class, loanapplyNo);
		return null != record?true:false;
	}
	
	public int insertArchiveRepeatRecord(ArchiveRecordRepeatEntity entity){
		if (entity == null) {
			return 0;
		}
		entity.setGdState(0);
		entity.setCreateTime(new Date());
		entity.setUpdateTime(new Date());
		return o2oJdbcSession.insert(entity);
	}
	
	public int updateArchiveRepeatRecord(ArchiveRecordRepeatEntity entity){
		if (entity == null) {
			return 0;
		}
		String sql;
		if(StringUtils.isNotBlank(entity.getContentJson())){
			sql="update o2o_archive_record_repeat set content_json =? where id=?";
			return o2oJdbcSession.execute(sql, new Object[]{entity.getContentJson(),entity.getId()});
		}else{
			sql="update o2o_archive_record_repeat set cw_content_json =? where id=?";
			return o2oJdbcSession.execute(sql, new Object[]{entity.getCwContentJson(),entity.getId()});
		}
	}
	
	public int deleteArchiveRepeatRecord(String id){
		if (StringUtils.isBlank(id)) {
			return 0;
		}
		String sql="update o2o_archive_record_repeat set gd_state =? where id=?";
		return o2oJdbcSession.execute(sql, new Object[]{1,id});
	}
	
	public int updateArchiveRepeatState(String gdId,int wfstate){
		String sql="update o2o_archive_record_repeat set wf_state =? where id=?";
		return o2oJdbcSession.execute(sql, new Object[]{wfstate,gdId});
	}
	
	
	public int saveSendArchiveRepeatInfo(ArchiveRecordRepeatEntity entity ){
		if (entity == null) {
			return 0;
		}
		String sql="update o2o_archive_record_repeat set send_time =?,send_company=?,send_no=? where id=?";
		return o2oJdbcSession.execute(sql, new Object[]{entity.getSendTime(),entity.getSendCompany(),entity.getSendNo(),entity.getId()});
	}
	
	public int savePtSendArchiveRepeatInfo(ArchiveRecordRepeatEntity entity ){
		if (entity == null) {
			return 0;
		}
		String sql="update o2o_archive_record_repeat set pt_send_time =?,pt_send_company=?,pt_send_no=? where id=?";
		return o2oJdbcSession.execute(sql, new Object[]{entity.getPtSendTime(),entity.getPtSendCompany(),entity.getPtSendNo(),entity.getId()});
	}
	
	
}
